﻿<UserControl x:Class="FR.SAFTAnalyser.Views.PaymentsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:v="clr-namespace:FR.SAFTAnalyser.SAFT.Validations"
             xmlns:toolkit="http://schemas.xceed.com/wpf/xaml/toolkit">
    <Grid MinWidth="600" MinHeight="600">
        <Grid.RowDefinitions>
            <RowDefinition Height="49*"/>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="49*"/>
        </Grid.RowDefinitions>

        <Grid.Resources>
            <CollectionViewSource x:Key="payments" Source="{Binding Payment, IsAsync=True}">
                <CollectionViewSource.GroupDescriptions>
                    <PropertyGroupDescription PropertyName="PaymentType"/>
                </CollectionViewSource.GroupDescriptions>
            </CollectionViewSource>
        </Grid.Resources>

        <GridSplitter Grid.Row="1" VerticalAlignment="Center" Background="DarkGray" ShowsPreview="True" Height="5" Width="Auto" HorizontalAlignment="Stretch" Margin="3" ResizeDirection="Rows" />

        <Grid Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="300"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="300"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <GridSplitter Grid.Column="1" Grid.RowSpan="2" HorizontalAlignment="Center" VerticalAlignment="Stretch" Background="DarkGray" ShowsPreview="True" Width="5" Height="Auto" Margin="3" ResizeDirection="Columns" />

            <DockPanel Grid.Column="0" Grid.Row="0">
                <GroupBox Header="Totais" DockPanel.Dock="Top">
                    <ScrollViewer VerticalScrollBarVisibility="Auto">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>

                            <TextBlock Text="Totais SAFT:" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3" FontSize="14" FontWeight="SemiBold" Margin="3"/>

                            <TextBlock Text="Documentos:" Grid.Column="0" Grid.Row="1" Margin="3" HorizontalAlignment="Right" />
                            <TextBlock Text="Total crédito:" Grid.Column="0" Grid.Row="2" Margin="3" HorizontalAlignment="Right" />
                            <TextBlock Text="Total débito:" Grid.Column="0" Grid.Row="3" Margin="3" HorizontalAlignment="Right" />
                            <TextBlock Text="Total:" Grid.Column="2" Grid.Row="1" Margin="3" HorizontalAlignment="Right" />
                            <TextBlock Text="Total s/ IVA:" Grid.Column="2" Grid.Row="2" Margin="3" HorizontalAlignment="Right" />
                            <TextBlock Text="Total IVA:" Grid.Column="2" Grid.Row="3" Margin="3" HorizontalAlignment="Right" />

                            <TextBlock Text="{Binding NumberOfEntries}" Grid.Column="1" Grid.Row="1" Margin="3" />
                            <TextBlock Text="{Binding TotalCredit}" Grid.Column="1" Grid.Row="2" Margin="3" />
                            <TextBlock Text="{Binding TotalDebit}" Grid.Column="1" Grid.Row="3" Margin="3" />
                            <TextBlock Text="{Binding GrossTotal}" Grid.Column="3" Grid.Row="1" Margin="3" />
                            <TextBlock Text="{Binding NetTotal}" Grid.Column="3" Grid.Row="2" Margin="3" />
                            <TextBlock Text="{Binding TaxPayable}" Grid.Column="3" Grid.Row="3" Margin="3" />

                            <TextBlock Text="Soma valores:" Grid.Column="0" Grid.Row="4" Grid.ColumnSpan="2" FontSize="14" FontWeight="SemiBold" Margin="3" />

                            <TextBlock Text="Documentos:" Grid.Column="0" Grid.Row="5" Margin="3" HorizontalAlignment="Right" />
                            <TextBlock Text="Total crédito:" Grid.Column="0" Grid.Row="6" Margin="3" HorizontalAlignment="Right" />
                            <TextBlock Text="Total débito:" Grid.Column="0" Grid.Row="7" Margin="3" HorizontalAlignment="Right" />
                            <TextBlock Text="Total:" Grid.Column="2" Grid.Row="5" Margin="3" HorizontalAlignment="Right" />
                            <TextBlock Text="Total s/ IVA:" Grid.Column="2" Grid.Row="6" Margin="3" HorizontalAlignment="Right" />
                            <TextBlock Text="Total IVA:" Grid.Column="2" Grid.Row="7" Margin="3" HorizontalAlignment="Right" />

                            <TextBlock Text="{Binding DocNumberOfEntries}" Grid.Column="1" Grid.Row="5" Margin="3" />
                            <TextBlock Text="{Binding DocTotalCredit}" Grid.Column="1" Grid.Row="6" Margin="3"/>
                            <TextBlock Text="{Binding DocTotalDebit}" Grid.Column="1" Grid.Row="7" Margin="3"/>
                            <TextBlock Text="{Binding DocGrossTotal}" Grid.Column="3" Grid.Row="5" Margin="3" />
                            <TextBlock Text="{Binding DocNetTotal}" Grid.Column="3" Grid.Row="6" Margin="3"/>
                            <TextBlock Text="{Binding DocTaxPayable}" Grid.Column="3" Grid.Row="7" Margin="3"/>

                        </Grid>
                    </ScrollViewer>
                </GroupBox>


                <Button Width="130" Height="25" ToolTip="Exportar para Excel" Margin="5" DockPanel.Dock="Bottom"
                        Command="{Binding DoOpenExcelCommand}">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="..\Resources\Images\excel.ico"/>
                        <TextBlock Text="Documento" Padding="5,0,0,0"/>
                    </StackPanel>
                </Button>
            </DockPanel>

            <DockPanel Grid.Column="2" Grid.RowSpan="2">
                <DockPanel DockPanel.Dock="Top">
                    <Label Content="Documentos" DockPanel.Dock="Left" Margin="3" />

                    <toolkit:DateTimePicker Value="{Binding FiltroDataInicio}" Format="Custom" FormatString="dd/MM/yyyy hh:mm:ss" />
                    <toolkit:DateTimePicker Value="{Binding FiltroDataFim}" Format="Custom" FormatString="dd/MM/yyyy hh:mm:ss"/>
                    <toolkit:WatermarkTextBox 
                        Margin="3" Watermark="Filtro por: No ou Cliente" HorizontalAlignment="Stretch"
                        Text="{Binding FilterPayment, UpdateSourceTrigger=PropertyChanged}"/>
                </DockPanel>

                <DataGrid x:Name="dataGridPayments" DockPanel.Dock="Bottom" TabIndex="1" VirtualizingPanel.IsVirtualizingWhenGrouping="True"
                          RowDetailsVisibilityMode="VisibleWhenSelected"
                          ItemsSource="{Binding Source={StaticResource payments}}" 
                          SelectedItem="{Binding CurrentPayment}">

                    <DataGrid.RowValidationRules>
                        <v:PaymentValidationRule ValidationStep="ConvertedProposedValue" ValidatesOnTargetUpdated="True"/>
                    </DataGrid.RowValidationRules>

                    <DataGrid.RowValidationErrorTemplate>
                        <ControlTemplate>
                            <Grid Margin="0,-2,0,-2"
                              ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}},
                            Path=(Validation.Errors)[0].ErrorContent}">
                                <Ellipse StrokeThickness="0" Fill="Red" 
                                     Width="{TemplateBinding FontSize}" 
                                     Height="{TemplateBinding FontSize}" />
                            </Grid>
                        </ControlTemplate>
                    </DataGrid.RowValidationErrorTemplate>

                    <DataGrid.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Mostrar Cliente" Command="{Binding ShowCustomerCommand}">
                                <MenuItem.Icon>
                                    <Image Source="{StaticResource Customer}"/>
                                </MenuItem.Icon>
                            </MenuItem>
                            <MenuItem Header="Abrir Documento Excel" Command="{Binding DoOpenExcelCommand}">
                                <MenuItem.Icon>
                                    <Image Source="{StaticResource Excel}"/>
                                </MenuItem.Icon>
                            </MenuItem>
                        </ContextMenu>
                    </DataGrid.ContextMenu>

                    <DataGrid.RowDetailsTemplate>
                        <DataTemplate>
                            <Expander IsExpanded="True" Padding="5,0,0,0">
                                <DataGrid x:Name="DetailPaymentMethod" AutoGenerateColumns="False" Padding="10,5,5,5"
                                          ItemsSource="{Binding PaymentMethod}">
                                    <DataGrid.Columns>
                                        <DataGridTextColumn Header="Tipo" Binding="{Binding PaymentMechanism}"/>
                                        <DataGridTextColumn Header="Data" Binding="{Binding PaymentDate, StringFormat=d}"/>
                                        <DataGridTextColumn Header="Quantia" Binding="{Binding PaymentAmount}"/>
                                    </DataGrid.Columns>
                                </DataGrid>
                            </Expander>
                        </DataTemplate>
                    </DataGrid.RowDetailsTemplate>

                    <DataGrid.Columns>
                        <DataGridTextColumn Header="No" Binding="{Binding PaymentRefNo}" Width="70"/>
                        <DataGridTextColumn Header="Estado" Binding="{Binding DocumentStatus.PaymentStatus}" Width="50" />
                        <DataGridTextColumn Header="Data estado" Binding="{Binding DocumentStatus.PaymentStatusDate, StringFormat=G}" />
                        <DataGridTextColumn Header="Data sistema" Binding="{Binding SystemEntryDate, StringFormat=G}" />
                        <DataGridTextColumn Header="Total s/ IVA" Binding="{Binding DocumentTotals.NetTotal}" Width="100"/>
                        <DataGridTextColumn Header="Total" Binding="{Binding DocumentTotals.GrossTotal}" Width="70"/>
                        <DataGridTextColumn Header="Total imposto" Binding="{Binding DocumentTotals.TaxPayable}" Width="100"/>
                        <DataGridTextColumn Header="Desconto" Binding="{Binding DocumentTotals.Settlement.SettlementAmount}" Width="100"/>
                        <DataGridTextColumn Header="Moeda" Binding="{Binding DocumentTotals.Currency.CurrencyCode}" Width="100"/>
                        <DataGridTextColumn Header="Quantia" Binding="{Binding DocumentTotals.Currency.CurrencyAmount}" Width="100"/>
                        <DataGridTextColumn Header="Câmbio" Binding="{Binding DocumentTotals.Currency.ExchangeRate}" Width="100"/>
                        <DataGridTextColumn Header="Periodo" Binding="{Binding Period}" Width="80"/>
                        <DataGridTextColumn Header="Descrição" Binding="{Binding Description}" Width="80"/>
                        <DataGridTextColumn Header="ID sistema" Binding="{Binding SystemID}" Width="80"/>
                        <DataGridTextColumn Header="Cliente" Binding="{Binding CustomerID}" Width="80"/>
                        <DataGridTextColumn Header="Motivo" Binding="{Binding DocumentStatus.Reason}" Width="100" />
                        <DataGridTextColumn Header="Utilizador" Binding="{Binding DocumentStatus.SourceID}" Width="100" />
                        <DataGridTextColumn Header="Data" Binding="{Binding TransactionDate, StringFormat=d}" />
                        <DataGridTextColumn Header="Origem" Binding="{Binding DocumentStatus.SourcePayment}" Width="50"/>
                        <DataGridTextColumn Header="Transacção" Binding="{Binding TransactionID}" />
                        <DataGridTextColumn Header="Data Transacção" Binding="{Binding TransactionDate, StringFormat=d}" />
                    </DataGrid.Columns>

                    <DataGrid.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.HeaderTemplate>
                                <DataTemplate>
                                    <StackPanel>
                                        <TextBlock Text="{Binding Path=Name}" />
                                    </StackPanel>
                                </DataTemplate>
                            </GroupStyle.HeaderTemplate>
                            <GroupStyle.ContainerStyle>
                                <Style TargetType="{x:Type GroupItem}">
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                                <Expander IsExpanded="True">
                                                    <Expander.Header>
                                                        <StackPanel Orientation="Horizontal">
                                                            <TextBlock Text="{Binding Path=Name}" />
                                                            <TextBlock Text=" ( " />
                                                            <TextBlock Text="{Binding Path=ItemCount}" />
                                                            <TextBlock Text=" )"/>
                                                        </StackPanel>
                                                    </Expander.Header>
                                                    <ItemsPresenter />
                                                </Expander>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </GroupStyle.ContainerStyle>
                        </GroupStyle>
                    </DataGrid.GroupStyle>
                </DataGrid>
            </DockPanel>

        </Grid>

        <Grid Grid.Row="2">
            <Grid.RowDefinitions>
                <RowDefinition Height="35"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <DockPanel Grid.Row="0">
                <CheckBox Content="Mostrar todas as linhas"  ToolTip="Mostrar as linhas de todos os documentos" DockPanel.Dock="Left" Margin="5,9,5,5"
                          IsChecked="{Binding ShowAllLines, Mode=TwoWay}"/>

                <Label Content="Linhas" DockPanel.Dock="Left" Margin="3" />

                <toolkit:WatermarkTextBox 
                        DockPanel.Dock="Right" Margin="3" TabIndex="2"
                        Watermark="Filtro por: Descrição produto ou Código produto ou Descrição" HorizontalAlignment="Stretch"
                        Text="{Binding FilterLines, UpdateSourceTrigger=PropertyChanged}"/>
            </DockPanel>

            <DataGrid x:Name="dataGridPaymentLines" Grid.Row="1"
                      ItemsSource="{Binding CurrentPaymentLines, IsAsync=True}"
                      RowDetailsVisibilityMode="VisibleWhenSelected"
                      SelectedItem="{Binding CurrentPaymentLine}">
                <DataGrid.RowValidationRules>
                    <v:PaymentLineValidationRule ValidationStep="ConvertedProposedValue" ValidatesOnTargetUpdated="True"/>
                </DataGrid.RowValidationRules>

                <DataGrid.RowValidationErrorTemplate>
                    <ControlTemplate>
                        <Grid Margin="0,-2,0,-2"
                              ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}},
                            Path=(Validation.Errors)[0].ErrorContent}">
                            <Ellipse StrokeThickness="0" Fill="Red" 
                                     Width="{TemplateBinding FontSize}" 
                                     Height="{TemplateBinding FontSize}" />
                        </Grid>
                    </ControlTemplate>
                </DataGrid.RowValidationErrorTemplate>

                <DataGrid.RowDetailsTemplate>
                    <DataTemplate>
                        <Expander IsExpanded="True" Padding="5,0,0,0">
                            <DataGrid x:Name="DetailPaymentLine" AutoGenerateColumns="False" Padding="10,5,5,5"
                                  ItemsSource="{Binding SourceDocumentID}">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Header="Origem" Binding="{Binding OriginatingON}"/>
                                    <DataGridTextColumn Header="Data" Binding="{Binding InvoiceDate, StringFormat=d}"/>
                                    <DataGridTextColumn Header="Descrição" Binding="{Binding Description}"/>
                                </DataGrid.Columns>
                            </DataGrid>
                        </Expander>
                    </DataTemplate>
                </DataGrid.RowDetailsTemplate>

                <DataGrid.Columns>
                    <DataGridTextColumn Header="Nº linha" Binding="{Binding LineNumber}" Width="60"/>
                    <DataGridTextColumn Header="Valor" Binding="{Binding Item}" />
                    <DataGridTextColumn Header="Tipo operação" Binding="{Binding ItemElementName}" />
                    <DataGridTextColumn Header="Desconto" Binding="{Binding SettlementAmount}" />
                    <DataGridTextColumn Header="Imposto" Binding="{Binding Tax.Item}"/>
                    <DataGridTextColumn Header="Isenção imposto" Binding="{Binding TaxExemptionReason}" />
                </DataGrid.Columns>
            </DataGrid>

        </Grid>
    </Grid>
</UserControl>
